package com.yashi.appController;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/fileUpLoad")
public class FileController{

    /**
	 * 文件下载
	 * @param res
	 * @param req
	 * @param id
	 * 
	 * 需要参数  fileurl
     * @throws UnsupportedEncodingException 
	 */
    @RequestMapping(value = "/download")
    public void download(HttpServletResponse res,HttpServletRequest req,String fileUrl) throws UnsupportedEncodingException {
    	String fileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1);
        String path=req.getServletContext().getRealPath("/fileupload/");
        res.setHeader("content-type", "application/octet-stream");
        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition","attachment;fileName=" +new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
          os = res.getOutputStream();
          bis = new BufferedInputStream(new FileInputStream(new File(path
              + fileName)));
          int i = bis.read(buff);
          while (i != -1) {
            os.write(buff, 0, buff.length);
            os.flush();
            i = bis.read(buff);
          }
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          if (bis != null) {
            try {
              bis.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
      }
}